package loteria.servidor;

import java.util.Properties; 

import loteria.utils.Propiedades;

import org.omg.CORBA.ORB;
import org.omg.CosNaming.NamingContextExt;
import org.omg.CosNaming.NamingContextExtHelper;
import org.omg.PortableServer.POA;
import org.omg.PortableServer.POAHelper;



public class Servidor {
	
	static final  String HOST = Propiedades.getValue("host");
	static final  String PORT = Propiedades.getValue("port");
	
	
    public static void main(String[] args) {
    	 Properties properties = System.getProperties();
         properties.put("org.omg.CORBA.ORBInitialHost", Servidor.HOST );
         properties.put("org.omg.CORBA.ORBInitialPort", Servidor.PORT);
        
         try {
             // Initialize the ORB
             ORB orb = ORB.init(args, properties);

             // Get root POA instance
             POA rootpoa = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
             
             // Activate root POA to accept requests
             rootpoa.the_POAManager().activate();

             // Create the servant object
             SorteoImpl sorteo = new SorteoImpl();                     
             // Register the object in the root POA
             // Get a reference for the object
             org.omg.CORBA.Object ref = rootpoa.servant_to_reference(sorteo);
             
             // Get a reference of the Naming Service (root Naming Context)
             org.omg.CORBA.Object nc_ref = orb.resolve_initial_references("NameService");
             
             // Use NamingContextExt (Interoperable Naming Service)
             NamingContextExt nc = NamingContextExtHelper.narrow(nc_ref);
             
             // Register the Chat object in the naming service
             nc.rebind(nc.to_name("Sorteo"), ref);
             
                          
             System.out.println(Thread.currentThread().getName() + ": Todo preparado en cuanto se conecten los observadores dará comienzo el sorteo....");      
             orb.run();
             
             
             
             
             
             
     }

     catch (Exception e) {
             System.err.println("ERROR: " + e);
             e.printStackTrace(System.out);
     }

     System.out.println("ChatServer exiting...");
    	
    }
        public Servidor(){

        	
        }
}
